/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgClientiSearch.java
 *
 * Created on 1-ago-2011, 16.24.49
 */
package UI.clienti.dialogs;

import UI.frmClienti;
import UI.frmCommesse;
import classes.Cliente;
import classes.Commessa;
import com.mysql.jdbc.Connection;
import database.DbConnection;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mrossetti
 */
public class dlgClientiSearch extends javax.swing.JDialog {

    /** Creates new form dlgClientiSearch */
    DbConnection Db;
    frmClienti frmParent;
    frmCommesse frmParent2;
    public dlgClientiSearch(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        if(parent.equals(frmParent)){
            frmParent = (frmClienti)parent;
        }else{
            frmParent2 = (frmCommesse)parent;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        P = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNominativo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtIndirizzo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        SP = new javax.swing.JScrollPane();
        TabRes = new javax.swing.JTable();
        cmdOk = new javax.swing.JButton();
        cmdEsci = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nominativo");

        txtNominativo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNominativoKeyPressed(evt);
            }
        });

        jLabel2.setText("Indirizzo");

        txtIndirizzo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIndirizzoKeyPressed(evt);
            }
        });

        jLabel3.setText("Risultati");

        TabRes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Cliente", "Nominativo", "Indirizzo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabRes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        TabRes.getTableHeader().setReorderingAllowed(false);
        SP.setViewportView(TabRes);

        cmdOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/ok.png.png"))); // NOI18N
        cmdOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOkActionPerformed(evt);
            }
        });

        cmdEsci.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/cancel.png"))); // NOI18N
        cmdEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEsciActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PLayout = new javax.swing.GroupLayout(P);
        P.setLayout(PLayout);
        PLayout.setHorizontalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 411, Short.MAX_VALUE)
                        .addComponent(cmdEsci))
                    .addComponent(SP, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNominativo, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                    .addComponent(txtIndirizzo, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addGap(12, 12, 12))
        );
        PLayout.setVerticalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNominativo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIndirizzo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SP, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                .addGap(14, 14, 14)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdOk)
                    .addComponent(cmdEsci))
                .addContainerGap())
        );

        getContentPane().add(P, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEsciActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cmdEsciActionPerformed

    private void txtNominativoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNominativoKeyPressed
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                doSearch();
            }
        });
    }//GEN-LAST:event_txtNominativoKeyPressed

    private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOkActionPerformed
        try{
            if(TabRes.getSelectedRow() >= 0){
                Cliente local = new Cliente();
                local.setIDCLiente(Integer.parseInt(TabRes.getValueAt(TabRes.getSelectedRow(), 0).toString()));
                if(frmParent != null){
                    frmParent.setClienteClass(local);
                }else{
                    Commessa localCom = frmParent2.getCommessa();
                    localCom.setCliente(local);
                    frmParent2.setCommessa(localCom);
                }
            }
            setVisible(false);
            dispose();
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_cmdOkActionPerformed

    private void txtIndirizzoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIndirizzoKeyPressed
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                doSearch();
            }
        });
    }//GEN-LAST:event_txtIndirizzoKeyPressed

    
    private void doSearch(){
        String SQL = "";
        String Nom = "";
        String Ind = "";
        
        if(txtNominativo.getText().trim().equals("")){
            Nom = "%";
        }else{
            Nom = "%" + txtNominativo.getText().trim().replaceAll("'", "''").replaceAll(" ", "%") + "%";
        }
        if(txtIndirizzo.getText().trim().equals("")){
            Ind = "%";
        }else{
            Ind = "%" + txtIndirizzo.getText().trim().replaceAll("'", "''").replaceAll(" ", "%") + "%";
        }
        SQL = "SELECT * from CLIENTI WHERE RagSoc like '" + Nom + "' AND Indirizzo LIKE '" + Ind + "'";
        try{
           Db = new DbConnection();
           ResultSet rs = Db.getResultSet(SQL);
           ((DefaultTableModel)TabRes.getModel()).setRowCount(0);
           while(rs.next()){
               ((DefaultTableModel)TabRes.getModel()).addRow(new Object[TabRes.getColumnCount()]);
               TabRes.setValueAt(rs.getString("IDCliente"), TabRes.getRowCount()-1, 0);
               TabRes.setValueAt(rs.getString("RagSoc"), TabRes.getRowCount()-1, 1);
               TabRes.setValueAt(rs.getString("Indirizzo"), TabRes.getRowCount()-1, 2);
           }
           rs.close();
           Db.CloseConnection();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel P;
    private javax.swing.JScrollPane SP;
    private javax.swing.JTable TabRes;
    private javax.swing.JButton cmdEsci;
    private javax.swing.JButton cmdOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txtIndirizzo;
    private javax.swing.JTextField txtNominativo;
    // End of variables declaration//GEN-END:variables
}
